﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ferreteria.Forms.GestionCategorias
{
    public partial class FrmAgregarCateg : Form
    {
        DisancoEntities context;
        public FrmAgregarCateg()
        {
            InitializeComponent();
            context = new DisancoEntities(Ferreteria.Clases.Constantes.cadenaConexion);
            cmbRubros.DataSource = context.Rubros;
            cmbRubros.DisplayMember = "rubro_nombre";
            cmbRubros.SelectedIndex = 1;
            context = null;
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bttnAceptar_Click(object sender, EventArgs e)
        {
            context = new DisancoEntities(Ferreteria.Clases.Constantes.cadenaConexion);
            if (txtbCategNombre.Text != null)
            {
                Categorias CategNueva = new Categorias();
                
                String RubroNom = cmbRubros.Text;
                Rubros rubro = context.Rubros.FirstOrDefault(r => r.rubro_nombre.Contains(RubroNom));
                CategNueva.rubro_id = rubro.rubro_id;
                CategNueva.categ_nombre = txtbCategNombre.Text;
                context.Categorias.AddObject(CategNueva);
                context.SaveChanges();
                DialogResult result = MessageBox.Show("La Categoria ha sido Guardada con Exito ",
                                                                      "Disanco Sanitarios", MessageBoxButtons.OK);

               this.Close();
            }
            context = null;
        }

        
    }
}
